package component

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.lt.load_the_image.rememberImagePainter
import constants.blueTeam
import constants.blueTeamColor
import constants.redTeam
import constants.redTeamColor
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import serializer.BigDecimalSerializer
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @description 排行榜
 * @author      jankinwu
 * @date        2024/4/12 23:03
 */
@Serializable
data class RankListPlayDTO(
    val openId: String,
    val uname: String,
    val avatarUrl: String = "",
    val teamColor: String = "",
    @Serializable(BigDecimalSerializer::class)
    val score: BigDecimal,
)

@Serializable
data class RankListDTO(
    val display: Boolean = false,
    val currentRoundScore: List<RankListPlayDTO> = ArrayList(),
    val totalScore: List<RankListPlayDTO> = ArrayList(),
    val contributionValue: List<RankListPlayDTO> = ArrayList(),
    val matchScore: List<RankListPlayDTO> = ArrayList()
)

var rankListDTO = mutableStateOf(RankListDTO());

@Composable
fun SingleColumnTable(rows: List<RankListPlayDTO>, title: String) {
    Box(
        modifier = Modifier
            .width(400.dp)
            .border(BorderStroke(2.dp, Color(0xFF341F1C)), shape = RoundedCornerShape(3.dp))
    ) {
        Column(
            modifier = Modifier.align(Alignment.Center)
        ) {
            TableTitle(title)
            rows.forEachIndexed { index, row ->
                TableRowItem(row.uname, row.avatarUrl, row.score, row.teamColor, isEven = index % 2 == 0, index)
            }
        }
    }
}

@Composable
private fun TableTitle(title: String) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color(0xFFB4967B))
            .padding(top = 4.dp, bottom = 4.dp),
        horizontalArrangement = Arrangement.Center
    ) {
        Text(
            text = title,
            color = Color(0xFF341F1C),
            fontFamily = FontFamily.Monospace,
            fontWeight = FontWeight.ExtraBold,
            fontSize = (19).sp,
            softWrap = false,
            overflow = TextOverflow.Visible,
            modifier = Modifier
                .background(Color.Transparent)
//            .width(IntrinsicSize.Max)
//                    .widthIn(min = textWidth.dp)
        )
    }
}

@Composable
private fun TableRowItem(
    uname: String,
    avatarUrl: String,
    score: BigDecimal,
    teamColor: String,
    isEven: Boolean,
    index: Int
) {
    val spacerWidth = 5

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(if (isEven) Color(0xFFD5B393) else Color(0xFFBA9A7D))
            .padding(vertical = 4.dp),
        contentAlignment = Alignment.CenterStart
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Row(
                modifier = Modifier.width(300.dp),
//                    .align(Alignment.TopStart)
//                    .wrapContentWidth(),
                horizontalArrangement = Arrangement.Start
                //                .widthIn(min = textWidth.dp)
                //                .offset(x = offsetTransition.value.x.dp)
            ) {
                // 圆形头像框
                Box(
                    modifier = Modifier
                        .size(27.dp)
                        .padding(start = 8.dp)
                        .background(Color.Transparent, shape = CircleShape),
                    contentAlignment = Alignment.Center
                ) {
                    Image(
                        painter = rememberImagePainter(avatarUrl),
                        contentDescription = "",
                        modifier = Modifier
                            .clip(CircleShape),
                    )
                }

                Spacer(modifier = Modifier.width(spacerWidth.dp))

                Text(
                    text = uname,
                    //                color = Color(0xFF341F1C),
                    color = when (teamColor) {
                        redTeam -> redTeamColor
                        blueTeam -> blueTeamColor
                        else -> Color(0xFF341F1C)
                    },
                    fontWeight = FontWeight.ExtraBold,
                    fontFamily = FontFamily.Monospace,
                    fontSize = (17).sp,
                    softWrap = false,
                    overflow = TextOverflow.Ellipsis,
//                    modifier = Modifier.width(30.dp)
                )
            }
            Text(
                text = score.setScale(0, RoundingMode.HALF_UP).toString(),
                color = when (teamColor) {
                    redTeam -> redTeamColor
                    blueTeam -> blueTeamColor
                    else -> Color(0xFF341F1C)
                },
                fontFamily = FontFamily.Monospace,
                fontWeight = FontWeight.ExtraBold,
                fontSize = (17).sp,
                softWrap = false,
                overflow = TextOverflow.Visible,
                modifier = Modifier.padding(end = 8.dp)
            )
        }
    }
}

@Composable
fun RankList() {
    Box(
        modifier = Modifier
            .fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        val currentRoundScoreList by remember { mutableStateOf(rankListDTO.value.currentRoundScore) }
        val matchScoreList by remember { mutableStateOf(rankListDTO.value.matchScore) }
        val totalScoreList by remember { mutableStateOf(rankListDTO.value.totalScore) }
        val contributionValueList by remember { mutableStateOf(rankListDTO.value.contributionValue) }
        Image(
            painter = rememberImagePainter("image/rank_list_backend.png"),
            contentDescription = "",
            modifier = Modifier
                .fillMaxSize(),
            contentScale = ContentScale.Crop
        )
        Image(
            painter = rememberImagePainter("image/header_ornaments.png"),
            contentDescription = "",
            modifier = Modifier
                .width(600.dp)
                .padding(top = 80.dp)
                .align(Alignment.TopCenter),
//            contentScale = ContentScale.Crop
        )
        Text(
            text = "排行榜",
            color = Color(0xFF341F1C),
            fontFamily = FontFamily.Monospace,
            fontWeight = FontWeight.Bold,
            fontSize = (30).sp,
            softWrap = false,
            overflow = TextOverflow.Visible,
            modifier = Modifier
                .padding(top = 100.dp)
                .align(Alignment.TopCenter)
        )
        Row(
            modifier = Modifier
                .padding(top = 190.dp)
                .align(Alignment.TopCenter),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            SingleColumnTable(totalScoreList, "出兵积分总榜")
            Spacer(modifier = Modifier.width(32.dp))
//            SingleColumnTable(currentRoundScoreList, "本局出兵积分榜")
            SingleColumnTable(matchScoreList, "比赛积分榜")
            Spacer(modifier = Modifier.width(32.dp))
            SingleColumnTable(contributionValueList, "贡献总榜")
        }
        Text(
            text = "注：出兵积分总榜和贡献总榜数据记录起始日期为2024年4月14日",
            color = Color(0xFF341F1C),
            fontFamily = FontFamily.Monospace,
            fontWeight = FontWeight.Bold,
            fontSize = (20).sp,
            softWrap = false,
            overflow = TextOverflow.Visible,
            modifier = Modifier
                .padding(end = 200.dp, bottom = 40.dp)
                .align(alignment = Alignment.BottomEnd)

        )

    }
}

@Composable
fun RankScreen() {
    val rankListDTOState by remember { mutableStateOf(rankListDTO) }
    Box(
        modifier = Modifier
            .fillMaxSize()
    ) {
        if (rankListDTOState.value.display) {
            RankList()
        }
    }
}

fun handleRankList(data: String) {
    rankListDTO.value = Json.decodeFromString<RankListDTO>(data)
}

fun handleRankList() {
    val data = "{\"contributionValue\":[{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/27e1be197f60adf9b6627d032ef06e766c6ac552.jpg\",\"openId\":\"39b950018e1a47d7a7bf97f7b52a204c\",\"score\":4759.5,\"uname\":\"上段视频营\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/bd5e6cf9f304521f90d0844e774d7008baac6214.jpg\",\"openId\":\"e8c90329123a4df7902487be05e90d48\",\"score\":4381.5,\"uname\":\"小de蜗牛\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/708a4124e63fe800d4774966b64824f8d1fd9b64.jpg\",\"openId\":\"ab5bfbf5950446b1a4ff27b7b2b50a3a\",\"score\":3774.0,\"uname\":\"四川豆花饭\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/a17079f4598c5679384606d3701e28bc7db3011a.jpg\",\"openId\":\"a133204cb99e4c91961fe86becceae1f\",\"score\":3642.0,\"uname\":\"柠檬berry\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/41ee9796c704c18692d7e3f7b9b60a3fc4d138bb.jpg\",\"openId\":\"a56abe9abfdf4dc1ba6dcfdb4b8f84f1\",\"score\":2808.0,\"uname\":\"野生懒羊羊大王\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/member/noface.jpg\",\"openId\":\"90613fd0d2634e78a8f08ce2b1e793d8\",\"score\":2482.5,\"uname\":\"狼哥davidkhala\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/e4532fe610ec217a40b723d02f2ccb74c7a5ed88.jpg\",\"openId\":\"3bf6ea98c565401eab6c268ca9855208\",\"score\":2377.5,\"uname\":\"崔精\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/207943943cb88a149a66498985c35686ce7d1926.jpg\",\"openId\":\"f2ef1a97284f48aa846444f1dc6ac8f6\",\"score\":2265.0,\"uname\":\"羽田蓝天\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/3c5cf8ded01186c4f4d7a57e340c63bc261cb8c9.jpg\",\"openId\":\"3644de484dce42b591f417b1cfc22a8c\",\"score\":2080.5,\"uname\":\"-奕豪-\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/816f97efbbec930ec2b26989afedd95506bd413d.jpg\",\"openId\":\"be3e691877004d6ead98a3177f288400\",\"score\":1743.0,\"uname\":\"茶叶冰袋er\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/0225c105ec9cf174fac5c38c3930084e24cc973b.jpg\",\"openId\":\"771db27ace974d37886a0fcd4c4193e9\",\"score\":1281.0,\"uname\":\"冰淇淋姬\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/747f59718a81947f966c82c74145fdc220539412.jpg\",\"openId\":\"052ec9f76c4d405d9670664c69c90bcd\",\"score\":1254.0,\"uname\":\"kkkk凯乐文\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/member/noface.jpg\",\"openId\":\"c65fad1d1f0d435fa18f63b147875709\",\"score\":1225.5,\"uname\":\"GG打爆狗秋风\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/654b91411f737e1dd275f7e4cd1336364222fee5.jpg\",\"openId\":\"6c8308fa743b458da1377ef5dd4dc9ff\",\"score\":1135.5,\"uname\":\"_君枫_\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/b6434b21775a7906843f1c6f838333a9f4df3c27.jpg\",\"openId\":\"bcbae4ae88704e6abcbcc49b60adb7b5\",\"score\":717.0,\"uname\":\"迦勒底的救世主\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/2fa371de4385ba98700962fb654762043754fd0e.jpg\",\"openId\":\"6439f5771b064c568e6882e0b2470eed\",\"score\":705.0,\"uname\":\"枫叶大帅\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/369a2126408d7de54cb1253bfba5905817db57aa.webp\",\"openId\":\"f3c34acfd4da41378a20a780393e6472\",\"score\":690.0,\"uname\":\"Cora烧鹅\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/5ead7df117fc617b8d0d028e927142b58564948b.jpg\",\"openId\":\"5f86e81db5d8415f8aa64d0178f45779\",\"score\":585.0,\"uname\":\"ミ琴诗\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/8d7a0d1e4e87ac4604a5e8d5a5334eb1cfe393aa.jpg\",\"openId\":\"c6e4c5b7823d469d8ba253c29218f3a3\",\"score\":541.5,\"uname\":\"MIKU夜乐\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/143f4c898ce17f033a518432eb4d7338373304cb.jpg\",\"openId\":\"cdd7685a3b5f48a6bdccdd918cd5b721\",\"score\":396.0,\"uname\":\"万恶的喵仆桑\"}],\"currentRoundScore\":[{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/8dcda8cc51f125f739d0defb5d6e943a66e55669.jpg\",\"openId\":\"44127ee8241f42be88ee57ddffb0c1ab\",\"score\":442,\"teamColor\":\"BLUE\",\"uname\":\"六等星de夜\"}],\"display\":true,\"matchScore\":[{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/8dcda8cc51f125f739d0defb5d6e943a66e55669.jpg\",\"openId\":\"44127ee8241f42be88ee57ddffb0c1ab\",\"score\":1088,\"uname\":\"六等星de夜\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/f2c2e70cee58166b5cc4041a0af5fea6467fe9b6.jpg\",\"openId\":\"86fda6405f4445939072840a4f3c98ce\",\"score\":35,\"uname\":\"雨落萧然X\"},{\"openId\":\"c9006f86c7424511ba656d409ea2be94\",\"score\":15},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/816f97efbbec930ec2b26989afedd95506bd413d.jpg\",\"openId\":\"be3e691877004d6ead98a3177f288400\",\"score\":7,\"uname\":\"茶叶冰袋er\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/37f386da7f510f089a634371589bf29dbb1e3b22.jpg\",\"openId\":\"b277f10c54ad42e1b11ea2fbbd7ca809\",\"score\":0,\"uname\":\"大笨蛋是大傻瓜\"},{\"openId\":\"7c0495dda794469a9a4b48e76bd49a55\",\"score\":0},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/a17079f4598c5679384606d3701e28bc7db3011a.jpg\",\"openId\":\"a133204cb99e4c91961fe86becceae1f\",\"score\":0,\"uname\":\"柠檬berry\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/3c5cf8ded01186c4f4d7a57e340c63bc261cb8c9.jpg\",\"openId\":\"3644de484dce42b591f417b1cfc22a8c\",\"score\":0,\"uname\":\"-奕豪-\"},{\"openId\":\"84512c8a27e6478ea9dc0413156004b3\",\"score\":0},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/c31f115bd6e2504a2733a07bc0f9339a27512e0d.jpg\",\"openId\":\"39496686faec4d1e82f34a84139710cf\",\"score\":0,\"uname\":\"友人Abandon\"}],\"totalScore\":[{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/37f386da7f510f089a634371589bf29dbb1e3b22.jpg\",\"openId\":\"b277f10c54ad42e1b11ea2fbbd7ca809\",\"score\":25441,\"uname\":\"大笨蛋是大傻瓜\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/816f97efbbec930ec2b26989afedd95506bd413d.jpg\",\"openId\":\"be3e691877004d6ead98a3177f288400\",\"score\":24934,\"uname\":\"茶叶冰袋er\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/a17079f4598c5679384606d3701e28bc7db3011a.jpg\",\"openId\":\"a133204cb99e4c91961fe86becceae1f\",\"score\":16771,\"uname\":\"柠檬berry\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/3c5cf8ded01186c4f4d7a57e340c63bc261cb8c9.jpg\",\"openId\":\"3644de484dce42b591f417b1cfc22a8c\",\"score\":14170,\"uname\":\"-奕豪-\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/bd5e6cf9f304521f90d0844e774d7008baac6214.jpg\",\"openId\":\"e8c90329123a4df7902487be05e90d48\",\"score\":12121,\"uname\":\"小de蜗牛\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/c31f115bd6e2504a2733a07bc0f9339a27512e0d.jpg\",\"openId\":\"39496686faec4d1e82f34a84139710cf\",\"score\":10961,\"uname\":\"友人Abandon\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/8dcda8cc51f125f739d0defb5d6e943a66e55669.jpg\",\"openId\":\"44127ee8241f42be88ee57ddffb0c1ab\",\"score\":10050,\"uname\":\"六等星de夜\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/bc538eceb13755c90e232db9b0241b9333f7c5a4.jpg\",\"openId\":\"2044766fd8f3478583d9409585eaba1d\",\"score\":9380,\"uname\":\"DR丶士道\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/e4532fe610ec217a40b723d02f2ccb74c7a5ed88.jpg\",\"openId\":\"3bf6ea98c565401eab6c268ca9855208\",\"score\":9250,\"uname\":\"崔精\"},{\"avatarUrl\":\"http://i0.hdslb.com/bfs/face/member/noface.jpg\",\"openId\":\"2f80656e7558426eb5f97d6ea4d162ed\",\"score\":8812,\"uname\":\"bili_61916362726\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/7f9fe3e6b267ab0511b8ce03f383635dcdbb41b5.jpg\",\"openId\":\"8b690176e1694737ace311be592468de\",\"score\":8781,\"uname\":\"用300APM抽十连的瞎子\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/ca9ceefc0eb94eeac39235cde781bae65db43521.jpg\",\"openId\":\"fa4d160d2af4477788c2ab81785d3d82\",\"score\":8561,\"uname\":\"辣椒不太甜-\"},{\"avatarUrl\":\"https://i1.hdslb.com/bfs/face/a657324cbe3a8f254eb325e6c17bfa6d8ae8823f.jpg\",\"openId\":\"a826a362b3b5482093e27792e227479f\",\"score\":7814,\"uname\":\"明镜心湖\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/605449e030d5bb04d40029ff1fb3263f8b715186.jpg\",\"openId\":\"5d71a3023c5c49da95667009e18f11ac\",\"score\":7738,\"uname\":\"碧血丹心abc\"},{\"avatarUrl\":\"http://i1.hdslb.com/bfs/face/ab3298f50e2d1cc6f2f5bb83e45a17c46892f47e.jpg\",\"openId\":\"2e2192b51fcd400db57ef7e56104a650\",\"score\":7670,\"uname\":\"一剑通慧\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/0225c105ec9cf174fac5c38c3930084e24cc973b.jpg\",\"openId\":\"771db27ace974d37886a0fcd4c4193e9\",\"score\":7666,\"uname\":\"冰淇淋姬\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/632cda9d916f2b794fa8907027457115f94c6014.jpg\",\"openId\":\"e4ad0957b8d842a08a4b29461887cdb0\",\"score\":7244,\"uname\":\"吨吨喝可乐的小柴神\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/f2c2e70cee58166b5cc4041a0af5fea6467fe9b6.jpg\",\"openId\":\"86fda6405f4445939072840a4f3c98ce\",\"score\":6338,\"uname\":\"雨落萧然X\"},{\"avatarUrl\":\"https://i0.hdslb.com/bfs/face/27e1be197f60adf9b6627d032ef06e766c6ac552.jpg\",\"openId\":\"39b950018e1a47d7a7bf97f7b52a204c\",\"score\":5614,\"uname\":\"上段视频营\"},{\"avatarUrl\":\"https://i2.hdslb.com/bfs/face/654b91411f737e1dd275f7e4cd1336364222fee5.jpg\",\"openId\":\"6c8308fa743b458da1377ef5dd4dc9ff\",\"score\":5391,\"uname\":\"_君枫_\"}]}"
    println(data)
    rankListDTO.value = Json.decodeFromString<RankListDTO>(data)
}